//
//  Scan.swift
//  swifttest1
//
//  Created by kologe on 2024/9/16.
//  description: 扫描与扫描被发现的协议功能

import Foundation
import SystemConfiguration

class Scan:BaseData{
    var protocolType:INT32;
     var hostName:String // 本设备的名称
     var ip:String       // 本设备IP
     var terminalType:String       // 本设备类型mac pc android
     
    
    override init(){
        ip="default"
        hostName=NetMessage.shared().hostName
        terminalType="default"
        protocolType = SCAN_MESSAGE_REQ
        super.init()
    }
    
    init(local:Bool){
        
        ip = NetMessage.shared().getIPAddress()
        hostName=NetMessage.shared().hostName
        terminalType = "MAC"
        protocolType = SCAN_MESSAGE_REQ
        super.init()
    }
    
    init(newScan:Scan){
        ip = newScan.ip
        hostName = newScan.hostName
        terminalType = newScan.terminalType
        protocolType = newScan.protocolType
        super.init()
    }
    
    /// CodingKeys init(from decoder: any Decoder)   func encode(to encoder: Encoder)  codable必须要实现的函数
    enum CodingKeys: String, CodingKey {
           case ip
           case hostName
           case terminalType
           case protocolType
    }
    required init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy:CodingKeys.self)
        ip = try container.decode(String.self, forKey: .ip)
        hostName = try container.decode(String.self, forKey: .hostName)
        terminalType = try container.decode(String.self, forKey: .terminalType)
        protocolType = try container.decode(INT32.self, forKey: .protocolType)
        
        try super.init(from: decoder)
//        fatalError("init(from:) has not been implemented")
    }
    
    //enCodeToJson会调用这个函数，这是codable需要实现的函数
    override func encode(to encoder: Encoder) throws {
            var container = encoder.container(keyedBy: CodingKeys.self)
            try container.encode(ip, forKey: .ip)
            try container.encode(hostName, forKey: .hostName)
            try container.encode(terminalType, forKey: .terminalType)
            try container.encode(protocolType, forKey: .protocolType)
            try super.encode(to: encoder)
    }
    
    ///收到不同类型的报文的不同操作
    static func receive_req(remoteScan:Scan){
        let localScan = Scan(local: true)
        localScan.protocolType = SCAN_MESSAGE_ANS
        let udpClient = UDPClient(host: remoteScan.ip, port: getUdpPort())
        udpClient.data = try! Scan.enCodeToJson(obj: localScan)
        udpClient.start()
    }
    
    static func receive_ans(remoteScan:Scan){
        DispatchQueue.main.async {
            terminal_list.add(obj:remoteScan)
        }
    }
    
    static func sendData(){
        let scan = Scan(local: false)
        let jsonData = try? Scan.enCodeToJson(obj: scan)
        var brocastAddressArr:[String] = getBroadcastAddresses()
        if(brocastAddressArr.isEmpty){
            brocastAddressArr.append("255.255.255.255")
        }
        let udpClient = UDPClient(host: brocastAddressArr[0], port: getUdpPort())
        udpClient.data=jsonData
        udpClient.start()
    }
    
    

}
